#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
struct Node {
  int pre, nxt;
} a[N];
bool flag = false;

void remove(int x) {
  a[a[x].pre].nxt = a[x].nxt;
  a[a[x].nxt].pre = a[x].pre;
}
void insert(int x, int y) {
  int z = a[y].nxt;
  a[x].nxt = z, a[z].pre = x;
  a[x].pre = y, a[y].nxt = x;
}
void mov_left(int x, int y) {
  remove(x);
  if (flag)
    insert(x, y);
  else
    insert(x, a[y].pre);
}
void mov_right(int x, int y) {
  remove(x);
  if (flag)
    insert(x, a[y].pre);
  else
    insert(x, y);
}
void swap_pos(int x, int y) {
  if (a[y].nxt == x) swap(x, y);
  int z = a[x].pre;
  remove(x);
  insert(x, y);
  remove(y);
  insert(y, z);
}
void reverse() { flag = !flag; }
void solve(int id) {
  flag = false;
  int head = n + 1, tail = n + 2;
  rep(i, 1, n) a[i].pre = i - 1, a[i].nxt = i + 1;
  a[head].nxt = 1, a[1].pre = head;
  a[tail].pre = n, a[n].nxt = tail;
  a[tail].nxt = 0;
  while (m--) {
    int op, x, y;
    cin >> op;
    if (op < 4) cin >> x >> y;
    if (op == 1) mov_left(x, y);
    if (op == 2) mov_right(x, y);
    if (op == 3) swap_pos(x, y);
    if (op == 4) reverse();
  }
  ll sum = 0;
  int p = a[head].nxt;
  if (flag && n % 2 == 0) p = a[p].nxt;
  while (p != tail && p) sum += p, p = a[a[p].nxt].nxt;
  cout << "Case " << id << ": " << sum << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int id = 0;
  while (cin >> n >> m) solve(++id);
  return 0;
}